<?php




/**
* Edit an entity in [ emails ]
*/

$emails = new emails();

# Handle Editing, when data is supplied
if($variable->post('edit-action', 'string', '') && ($email_id = $variable->post('email_id', 'integer', 0)))
{
	# Editing....
	$code = $variable->post('protection_code', 'string', '');
	$data = $variable->post('emails', 'array', array());
	
	# Mark when this data was modified last time.
	$data['modified_on'] = 'UNIX_TIMESTAMP(CURRENT_TIMESTAMP())';
	$data['modified_counter'] = 'modified_counter+1';
	
	# If there are some FILEs to upload, please do now.
	# And assign the name of the file returned, to the same field.
	# Old uploaded file is probably lost. So, delete it first.
	# $data['file_field'] = uploader::upload_delete($email_id);
	# $data['file_field'] = uploader::upload('file_field');
	
	# Working version: Be sure to delete old files first if available.
	# $uploader = new uploader(__BASE__.'/templates/images');
	# $file_field = $uploader->store('file_field', '');
	# if($file_field) $data['file_field'] = $file_field;
	
	# Produce the cropnail for uploaded images
	# $cropnail = new cropnail(200, 150); $cropnail->resize($original_image, $cropnail_image, 0);
	
	if($success = $emails->edit(
		$data, # Posted data
		array(
			'email_id' => $email_id,
		),
		$code, # Security code related to this entry
		$email_id
	))
	{
		# Something about the image uploaders as a patch, if applies
		# $cu = new customized_uploader('uploader', __BASE__.'/templates/images/emails', 'images/emails', $record_id=$email_id);
		
		$messenger = new messenger('success', 'The record has been modified.');
		
		stopper::url(url::last_page('emails-list.php?context=permissions'));
		#stopper::url('emails-edit-successful.php');
		#stopper::url('emails-list.php');
	}
	else
	{
		stopper::url('emails-edit-error.php');
	}
}
else
{
	/**
	* Otherwise, load the details of the entity before editing it.
	*/
	if($email_id = $variable->get('id', 'integer', 0))
	{
		$details = $emails->details($email_id);
		if(!$details)
		{
			# Data about this entity was not available
			stopper::url('emails-edit-error.php?context=data');
		}
		# Purpose of this code block is to make sure that the variable
		# gets all indices with blank data, to feed to EDIT form.
		$details = $emails->validate('edit', $details);

		/**
		* Build Smarty Variable with FULL details
		*/
		$smarty->assign('emails', $details);
	}
	else
	{
		# Really Bad...
		stopper::url('emails-direct-access-error.php');
	}
}
?>